package com.defect.controller.basics;

import com.defect.bo.ResultBo;
import com.defect.bo.RoleBo;
import com.defect.po.Role;
import com.defect.po.State;
import com.defect.po.User;
import com.defect.service.basicsService.BRoleService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("role")
public class BRoleController {
    @Resource
    private BRoleService roleService;

    /**
     * 查询所有角色
     *
     * @return
     */
    @GetMapping
    public ResultBo selectRoleAll() throws Exception {
        List<Role> list = roleService.selectRoleAll();
        List<Map<String, Object>> map = new ArrayList<Map<String, Object>>();
        for (Role role : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", role.getRoleCode());
            map1.put("name", role.getRoleName());
            map1.put("roleState", role.getModuleState());
            List<State> states = role.getList();
            String a = "";
            for (int i = 0; i < states.size(); i++) {
                if ((i + 1) == states.size()) {
                    a += (states.get(i).getStateDesc());
                } else {
                    a += (states.get(i).getStateDesc() + ",");
                }
            }
            map1.put("state", a);
            map.add(map1);
        }
        return new ResultBo("200", "success", null, map);
    }

    /**
     * 新增角色
     * @param role
     * @return
     * @throws Exception
     */
    @PostMapping
    public ResultBo insertRole(@RequestBody RoleBo role) throws Exception {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
//        User user=(User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        role.getRole().setModifyBy(user.getUserName());
//        role.getRole().setCreateBy(user.getUserName());
        role.getRole().setCreateTime(date);
        role.getRole().setModifyTime(date);
        role.getRole().setModuleState("1");
        roleService.insertRole(role);
        return new ResultBo("200", "success", null, null);
    }

    /**
     * 将角色状态改为禁用
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("updata1")
    public ResultBo updateRole1(String id) throws Exception {
        Role role = new Role();
        role.setRoleCode(id);
        role.setModuleState("0");
        roleService.updateRole(role);
        return new ResultBo("200", "success", null, null);
    }

    /**
     * 将角色状态改为有效
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("updata0")
    public ResultBo updateRole0(String id) throws Exception {
        Role role = new Role();
        role.setRoleCode(id);
        role.setModuleState("1");
        roleService.updateRole(role);
        return new ResultBo("200", "success", null, null);
    }

    /**
     * 根据id删除角色
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("delete")
    public ResultBo deleteRole(String id) throws Exception {
        roleService.deleteRole(id);
        return new ResultBo("200", "success", null, null);
    }

    @DeleteMapping
    public ResultBo deleteUsers(String[] arr) throws Exception {
        for (int i = 0; i <arr.length ; i++) {
            roleService.deleteRole(arr[i]);
        }
        return new ResultBo("200","success",null,null);
    }
}
